<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Book_Installer extends Engine_Package_Installer_Module {

    public function onInstall() {
        $this->_addBookDetailPage();
        $this->_addBookHomePage();
        parent::onInstall();
    }

    protected function _addBookHomePage() {
        $db = $this->getDb();

        // profile page
        $page_id = $db->select()
                ->from('engine4_core_pages', 'page_id')
                ->where('name = ?', 'book_index_index')
                ->limit(1)
                ->query()
                ->fetchColumn();

        // insert if it doesn't exist yet
        if (!$page_id) {
            // Insert page
            $db->insert('engine4_core_pages', array(
                'name' => 'book_index_index',
                'displayname' => 'Book Home Page',
                'title' => 'Book Home',
                'description' => 'This page displays all books entry.',
                'provides' => 'subject=book',
                'custom' => 0,
            ));
            $page_id = $db->lastInsertId();

            // Insert main
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'main',
                'page_id' => $page_id,
            ));
            $main_id = $db->lastInsertId();

            // Insert left
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'left',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $left_id = $db->lastInsertId();

            // Insert right
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'right',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $right_id = $db->lastInsertId();

            // Insert middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 2,
            ));
            $middle_id = $db->lastInsertId();

            // Insert content
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'core.content',
                'page_id' => $page_id,
                'parent_content_id' => $middle_id,
                'order' => 1,
            ));
        }
    }

    protected function _addBookDetailPage() {
        $db = $this->getDb();

        // profile page
        $page_id = $db->select()
                ->from('engine4_core_pages', 'page_id')
                ->where('name = ?', 'book_index_detailbook')
                ->limit(1)
                ->query()
                ->fetchColumn();

        // insert if it doesn't exist yet
        if (!$page_id) {
            // Insert page
            $db->insert('engine4_core_pages', array(
                'name' => 'book_index_detailbook',
                'displayname' => 'Book Detail Page',
                'title' => 'Book Detail',
                'description' => 'This page displays a book entry.',
                'provides' => 'subject=book',
                'custom' => 0,
            ));
            $page_id = $db->lastInsertId();

            // Insert main
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'main',
                'page_id' => $page_id,
            ));
            $main_id = $db->lastInsertId();

            // Insert left
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'left',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $left_id = $db->lastInsertId();

            // Insert right
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'right',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $right_id = $db->lastInsertId();

            // Insert middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 2,
            ));
            $middle_id = $db->lastInsertId();

            // Insert content
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'core.content',
                'page_id' => $page_id,
                'parent_content_id' => $middle_id,
                'order' => 1,
            ));

            //insert comment
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'core.comments',
                'page_id' => $page_id,
                'parent_content_id' => $middle_id,
                'order' => 2,
            ));
        }
    }

}
